<%@page import="com.stwitinc.util.IDGenerator"%>
<%@page import="com.alibaba.fastjson.JSONObject"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="stw" uri="http://stwitinc.com/jsp/tld/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑新闻</title>
<stw:import type="jquery" src="/jquery" />
<stw:import type="bootstrap" src="/bootstrap" />
<stw:import type="script" src="/kindeditor/kindeditor-all-min.js" />
<stw:import type="script" src="/aliyun/aliyun-oss-sdk.min.js" />
<script type="text/javascript">
	var contentEditor;
	KindEditor.ready(function(K) {
		contentEditor = K.create('textarea[name="content"]', {
			cssPath : 'kindeditor/plugins/code/prettify.css',
			allowImageRemote : false,
			getSecurityTokenURL : "${contextPath}/news/sys/oss_security_token?tokenId=${param.tokenId}",
			items : [ 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter',
					'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', 'clearhtml', 'quickformat', 'selectall', '|',
					'fullscreen', '/', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|',
					'image', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' ]
		});
		prettyPrint();
	});

	function getCurrentDate() {
		var d = new Date();
		var year = d.getFullYear();
		var month = d.getMonth() + 1;
		if (month < 10) {
			month = "0" + month;
		}
		var day = d.getDate();
		if (day < 10) {
			day = "0" + day;
		}
		return year + "" + month + "" + day;
	}

	function uploadTitleImage(file, newsId) {
		var opts = {
			url : "${contextPath}/news/sys/oss_security_token?tokenId=${param.tokenId}",
			type : "GET",
			dataType : "json",
			success : function(token) {
				var client = new OSS.Wrapper({
					accessKeyId : token.accessKeyId,
					accessKeySecret : token.accessKeySecret,
					stsToken : token.securityToken,
					region : token.ossRegion,
					bucket : token.ossBucket
				});
				file = file.files[0];
				var key = "stw_news/images/title_images/" + getCurrentDate() + "/" + newsId + getExtension(file.name);
				client.multipartUpload(key, file).then(function(result) {
					var url = client.signatureUrl(key);
					$("input[name='titleImageUri']").val(key);
					$("#titleImage").attr("src", url);
				}).catch(function(err) {
					alert(err.message);
				});
			}
		};
		$.ajax(opts);
	}
	function getExtension(fileName) {
		return fileName.substring(fileName.lastIndexOf("."));
	}
	function saveNews() {
		contentEditor.sync();
		$("#newsForm").submit();
	}
	function deleteNews() {
		$.ajax({
			url : "${contextPath}/news/news/${newsId}?tokenId=${param.tokenId}",
			type : "DELETE",
			dataType : "json",
			success : function(data) {
				if (data.error == 0) {
					window.location.href = "${contextPath}/news/news/news_management?tokenId=${param.tokenId}&state=${rec.state}";
				}
			}
		});
	}
</script>
</head>
<body>
	<ol class="breadcrumb">
		<li><a href="${contextPath}/news/user/logout?tokenId=${param.tokenId}">主页</a></li>
		<li><a href="${contextPath}/news/sys/management?tokenId=${param.tokenId}">功能</a></li>
		<li><a href="${contextPath}/news/news/news_management?tokenId=${param.tokenId}&state=${empty rec.state ? 1 : rec.state}">资讯管理</a></li>
		<li class="active">${isNew ? "新增资讯" : "修改资讯"}</li>
	</ol>
	<div class="panel panel-default" style="width: 800px;">
		<div class="panel-heading">编辑</div>
		<div class="panel-body">
			<form id="newsForm" class="form-horizontal" method="POST" action="${contextPath}/news/news/${newsId}?tokenId=${param.tokenId}&isNew=${isNew}&state=${rec.state}">
				<input type="hidden" name="titleImageUri" value="${rec.titleImageUri}">
				<div class="form-group">
					<label for="category" class="col-sm-2 control-label">分类：</label>
					<div class="col-sm-10">
						<select id="category" class="form-control" name="category">
							<option value="0" ${rec.category eq "0" ? "selected" : ""}>通用</option>
							<option value="1" ${rec.category eq "1" ? "selected" : ""}>医护资讯</option>
							<option value="2" ${rec.category eq "2" ? "selected" : ""}>患者资讯</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="title" class="col-sm-2 control-label">标题：</label>
					<div class="col-sm-10">
						<input id="title" class="form-control" type="text" name="title" value="${rec.title}">
					</div>
				</div>
				<div class="form-group">
					<label for="authorName" class="col-sm-2 control-label">作者名称：</label>
					<div class="col-sm-10">
						<input id="authorName" class="form-control" type="text" name="authorName" value="${rec.authorName}">
					</div>
				</div>
				<div class="form-group">
					<label for="titleImageFile" class="col-sm-2 control-label">标题图片：</label>
					<div class="col-sm-10">
						<input id="titleImageFile" type="file" accept=".jpg,.png,.gif" onchange="uploadTitleImage(this,'${newsId}');"> <img id="titleImage" class="img-thumbnail" height="64"
							src="${rec.presignedTitleImageUri}">
					</div>
				</div>
				<div class="form-group">
					<label for="authorName" class="col-sm-2 control-label">内容：</label>
					<div class="col-sm-10">
						<textarea name="content" rows="20" style="width: 100%;">${rec.content}</textarea>
					</div>
				</div>
				<div class="form-group">
					<div class="col-sm-offset-2 col-sm-10">
						<a href="#" class="btn btn-primary" onclick="saveNews();">保存</a> <a href="#" class="btn btn-danger" onclick="deleteNews();">删除</a>
					</div>
				</div>
			</form>
		</div>
	</div>
</body>
</html>